package it.patamau.bukkit.deathmatch;

import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityListener;

public class DMEntityListener extends EntityListener {

	private DMPlugin plugin;
	
	public DMEntityListener(DMPlugin plugin){
		this.plugin = plugin;
	}
	
	public void onEntityDamage(EntityDamageEvent event){
		Player damager = null;
		if(event instanceof EntityDamageByEntityEvent){
    		Entity e = ((EntityDamageByEntityEvent) event).getDamager();
    		if(e instanceof Player){
    			//only consider player damager, do not record other entities
    			damager = (Player)e;
    		}
    	}
		Entity e = event.getEntity();
		if(e instanceof Player){
			plugin.onDamage((Player)e, event.getCause(), damager);
		}
	}
	
	public void onEntityDeath(EntityDeathEvent event){
		Entity e = event.getEntity();
		if(e instanceof Player){
			plugin.onDeath((Player)e);
		}
	}
}
